#!/bin/bash
#
selinuxs(){
if getenforce &>/dev/null;then
	echo "selinux-status:close"
else
	sed -i '/^SELINUX=/s|enforcing|disabled|' /etc/selinux/config
	if [ $? -eq 0 ];then
		echo "selinux-close:success";
	else
		echo "selinux-close:error"
	fi
fi 
}

firewallds(){
if systemctl status firewalld &> /dev/null ; then
	systemctl stop firewalld &>/dev/null
	systemctl disable firewalld.service &>/dev/null
	if [ $? -eq 0 ];then
		echo "firewalld-close:success"
	else
		echo "firewalld-close:error"
	fi
else 
	echo "firewalld-status:close"
fi
}

DHCP_yum(){
if systemctl status dhcpd &>/dev/null;then
	echo "DHCP_yum:success"
else
	yum install -y dhcp &> /dev/null
	if [ $? -eq 0 ];then 
		echo "DHCP_yum:success"
	else
		echo "DHCP_yum:error"
	fi
fi
}
DHCP_config(){
ips=`ifconfig ens33|awk '/192/{gsub(/255/,0);print $6}'` &>/dev/null
prefix=`ifconfig ens33|awk '/192/{print $4}' `&>/dev/null
gateway=`awk -F= '/GATEWAY/{print $2}' /etc/sysconfig/network-scripts/ifcfg-ens33`&>/dev/null
read -p "请输入ip范围: (20 50)" fw 
fws=`echo "$ips"|awk -F'.' '{print $1"."$2"."$3"."}'`
fw1=$fws`echo "$fw"|awk '{print $1}'`
fw2=$fws`echo "$fw"|awk '{print $2}'`
ip=`ifconfig ens33|awk '/192/{print $2}'` &>/dev/null
echo -e "subnet $ips netmask $prefix {
	range $fw1 $fw2;
	option routers $gateway;
	option domain-name-servers $ip;
}"|awk '{print}' >a.txt ;mv a.txt /etc/dhcp/dhcpd.conf
}
DHCP_start(){
	if systemctl status dhcpd &>/dev/null;then
		echo "DHCP_start:success"
	else
        systemctl  start dhcpd &>/dev/null
        echo "DHCP_start:success"
	fi	
	systemctl enable dhcpd &>/dev/null
}
#前置配置
	#关闭selinux
	selinuxs
	#关闭防火墙
	firewallds
#初始化DHCP
	#安装DHCP
	DHCP_yum
	#修改配置文件
	DHCP_config
	#设置开机自启
	DHCP_start
